package vn.lumi.smarthome.ulti;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Comparator;

public class Utility {
//	private static final String TAG="Utility";
	public static String md5(String in) {
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.reset();
			digest.update(in.getBytes());
			byte[] a = digest.digest();
			int len = a.length;
			StringBuilder sb = new StringBuilder(len << 1);
			for (int i = 0; i < len; i++) {
				sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
				sb.append(Character.forDigit(a[i] & 0x0f, 16));
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
    public static String[] sortLoTo(String[] array)
    {
    	Arrays.sort(array, new CustomeStringComparator());
    	return array;
    }
    
    private static class CustomeStringComparator implements Comparator<String>
    {
		@Override
		public int compare(String lhs, String rhs) {
			int num1=Integer.parseInt(lhs);
			int num2=Integer.parseInt(rhs);
			return num1-num2;
		}
    }
    
	// Fast Implementation
	public static StringBuilder inputStreamToString(InputStream is)throws IOException {
	    String line = "";
	    StringBuilder total = new StringBuilder();
	    
	    // Wrap a BufferedReader around the InputStream
	    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

	    // Read response until the end
	    while ((line = rd.readLine()) != null) { 
	        total.append(line); 
	    }
	    
	    // Return full string
	    return total;
	}
	
}
